<template>
	<view class="my-total">
		<view class="one">
			<label class="radio">
				<radio :checked="allStatus" @click="changeRadio" value="true" color="#c00000" /><text>全选</text>
			</label>
		</view>
		<view class="two">
			合计：<text>￥{{totalPrice}}</text>
		</view>
		<view class="three" @click="paybtn">
			结算（{{total}}）
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-total",
		computed: {
			...mapGetters('cart', ['total', 'allStatus', 'totalPrice']),
			...mapState('address', ['token']),
			...mapState('cart', ['cart']),
			...mapGetters('address', ['fullAddress'])
		},
		data() {
			return {
				second: 3,
				timer: null
			};
		},
		methods: {
			...mapMutations('cart', ['changeStatus']),
			...mapMutations('address', ['getrRedirectInfo']),
			changeRadio() {
				this.changeStatus(!this.allStatus)
			},
			// 点击提交订单
			async paybtn() {
				if (this.total === 0) return uni.$showMsg('请选择商品')
				if (JSON.stringify(this.address) === '{}') return uni.$showMsg('请选择收货地址')
				if (!this.token) {
					// uni.$showMsg('请先登录')
					return this.delayNavigate()
				}

				// 获取支付订单
				let orderNum = await this.createOrder()
			},
			// 获取支付订单方法
			async createOrder() {
				let goods = this.cart.filter(item => item.goods_status).map(item => {
					return {
						goods_id: item.goods_id,
						goods_number: item.goods_count,
						goods_price: item.goods_price
					}
				})
				/* let res = await uni.$http.post('/api/public/v1/my/orders/create', {
					order_price: this.totalPrice,
					consignee_addr: this.fullAddress,
					goods
				}) */
				let {
					data: res
				} = await uni.$http.post('/api/public/v1/my/orders/create', {
					order_price: this.totalPrice,
					consignee_addr: this.fullAddress,
					goods
				})
				console.log(res);
			},
			showTip(n) {
				uni.showToast({
					title: `您还未登录，${n}秒后跳转登录`,
					icon: 'none',
					duration: 2000,
					mask: true
				})
			},
			delayNavigate() {
				if (this.timer) return
				this.second = 3
				this.showTip(this.second)
				this.timer = setInterval(() => {
					this.second--
					this.showTip(this.second)
					if (this.second === 0) {
						clearInterval(this.timer)
						this.timer = null
						uni.hideToast()
						this.getrRedirectInfo({
							openType: 'switchTab',
							url: '/pages/buycar/buycar'
						})

						uni.switchTab({
							url: '/pages/mine/mine'
						})
					}
				}, 1000)

			}
		}
	}
</script>

<style lang="scss">
	.my-total {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 999;
		background-color: #fff;
		display: flex;
		align-items: center;
		height: 100rpx;
		text-align: center;

		.one {
			flex: 2;
		}

		.two {
			flex: 5;

			text {
				color: #c00000;
			}
		}

		.three {
			flex: 3;
			height: 100rpx;
			line-height: 100rpx;
			color: #fff;
			background-color: #c00000;
		}
	}
</style>
